package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.StoreGoodsMapper;
import com.aaa.mrcai.pojo.entity.*;
import com.aaa.mrcai.pojo.vo.GoodsVO;
import com.aaa.mrcai.pojo.vo.StoreGoodsVO;
import com.aaa.mrcai.query.AddStoreGoodsQuert;
import com.aaa.mrcai.query.StoreGoodsQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.StoreGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author 惠普
 * @date 2021/12/9  20:37
 */
@Service
public class StoreGoodsServiceImpl implements StoreGoodsService {

    @Autowired
    private StoreGoodsMapper storeGoodsMapper;

    @Autowired
    private ServletContext servletContext;

    @Autowired
    private ServletContext servletContext2;


    @Override
    public AjaxResult searchList(StoreGoodsQuery storeGoodsQuery) {
        UserInfo user = (UserInfo) servletContext2.getAttribute("user");
        Integer bid = user.getUserLogin().getBid();
        storeGoodsQuery.setBid(bid);
        Map<String, Object> map = new HashMap<>();
        List<StoreGoods> storeGoods = storeGoodsMapper.searchList(storeGoodsQuery);
        Long aLong = storeGoodsMapper.queryGoodsTotalCount(storeGoodsQuery);
        map.put("currentData",storeGoods);
        map.put("currentDataTotal",aLong);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult putdate(StoreGoodsQuery storeGoodsQuery) {
        List<StoreGoods> putdate = storeGoodsMapper.putdate(storeGoodsQuery);
        return AjaxResult.success(putdate);
    }

    @Override
    public AjaxResult getOneGoods(int id) {
        StoreGoodsVO oneGoods = storeGoodsMapper.getOneGoods(id);
        return AjaxResult.success(oneGoods);
    }

    @Override
    public AjaxResult stateList() {

        List<GoodsState> goodsStates = storeGoodsMapper.stateList();
        return AjaxResult.success(goodsStates);
    }

    @Override
    public AjaxResult updateOneGoods(StoreGoodsVO storeGoodsVO) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        storeGoodsVO.setPutdate(date.format(formatter));
        int i = storeGoodsMapper.updateOneGoods(storeGoodsVO);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult getCid(StoreGoodsQuery storeGoodsQuery) {
        BussinessInfo bussinessInfo = storeGoodsMapper.getCid(storeGoodsQuery);
        Integer cid = bussinessInfo.getCid();
        /*System.out.println(cid);*/
        servletContext.setAttribute("businessInfo",bussinessInfo);
        /*System.out.println(bussinessInfo);*/
        return AjaxResult.success(cid);
    }

    @Override
    public AjaxResult addGoodsList(AddStoreGoodsQuert addStoreGoodsQuert) {
        Map<String, Object> map = new HashMap<>();
        List<Goods> goods = storeGoodsMapper.queryGoods(addStoreGoodsQuert);
        Long aLong = storeGoodsMapper.queryGoodsTotal(addStoreGoodsQuert);
        map.put("currentData",goods);
        map.put("currentDataTotal",aLong);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult addOneGoods(GoodsVO goodsVO) {
        BussinessInfo businessInfo = (BussinessInfo) servletContext.getAttribute("businessInfo");
        Integer sid = businessInfo.getSid();
        goodsVO.setSid(sid);

        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        goodsVO.setPutdate(date.format(formatter));
        Long goodsExists = storeGoodsMapper.isGoodsExists(goodsVO);
        if(goodsExists>0){
            return AjaxResult.defeated("您的店铺已存在该商品");
        }
        int i = storeGoodsMapper.addOneGoods(goodsVO);
        if(i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult deleteOneGoods(int id) {
        int i = storeGoodsMapper.deleteOneGoods(id);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult getState(int bid) {
        int state = storeGoodsMapper.getState(bid);
        System.out.println(state);
        if (state == 1){
            return AjaxResult.defeated("对不起，您的店铺已经停业整顿，不能进行操作");
        }
        return AjaxResult.success();
    }

}
